Skip to content

sys: clock: add sys_clock api and remove posix from iso c time #90096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented May 18, 2025

Previously, both the common libc time() as well as picolibc time() (an ISO C function) relied on either clock_gettime() or gettimeofday(). That had the unfortunate consequence that many areas had an indirect dependency on POSIX.

This change removes the dependency cycle that existed between POSIX and some libc's by creating a mutual dependency (i.e. new API) for both POSIX and the common C library under lib/os. With this change, the ISO C function time() can also be used without relying on POSIX support.

  • sys_clock_gettime()
  • sys_clock_settime()
  • sys_clock_nanosleep()
  • sys_clock_getrtoffset()

Areas that should no longer have POSIX dependencies for getting the "wall clock" time include, but are not limited to

  • libc
    • lib/libc/common/source/thrd
    • lib/libc/common/source/time
    • picolibc (specifically with time())
  • samples/net/cloud/aws_iot_mqtt
  • samples/net/lwm2m_client
  • subsys/logging/log_core.c
  • subsys/net/lib/config (sntp)
  • subsys/net/lib/lwm2m

Please see individual commits for details

  • scripts: ci: check_compliance: allow CONFIG_BOARD_UNIT_TESTING
  • posix: cond + mutex: log messages generate warnings for 32-bit timeouts
  • sys: clock: additional sys_clock api calls
  • doc: release: 4.2.0: add sys_clock gettime settime nanosleep notes
  • logging: use sys_clock_gettime()
  • libc: common: time: use sys_clock api rather than posix
  • libc: use the common libc time() implementation for most libcs
  • libc: common: thrd: use sys_clock_nanosleep() instead of nanosleep()
  • tests: libc: thrd: compare with thrd_success rather than ok or zero
  • tests: libc: thrd: do not pass NULL for thrd_sleep() duration
  • tests: libc: thrd: use timespec_from_timeout()
  • tests: libc: thrd: use sys_clock_gettime() and timespec_add()
  • tests: lib: move time testsuite to c_lib
  • posix: use sys_clock implementation
  • net: remove dependency on posix for iso c time() function
  • samples: net: remove POSIX_TIMERS and XSI_SINGLE_PROCESS
  • sys: clock: merge sys_clock.h into sys/clock.h

Fixes #88555
Fixes #88882

Note

There have been many RFCs on this topic already #19030, #40099, #76335 and most of them agree on the need for an abstract clock source at the OS level. There isn't a huge need for another RFC here.

  • this PR is fixing a bug at this point, but otherwise should facilitate the improvements mentioned in previous RFCs
  • rather than the proposed K_ prefix, the SYS_ prefix was suggested by @peter-mitsis
  • there is still a distinct need to communicate clock resolution in a better way (does not need to happen in this PR)
  • suggestion for above is to use a dt-chosen node that simply provides metadata for the system uptime counter (resolution, accuracy, constness) and then wrap it in sys_clock_getres().

@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch 2 times, most recently from a403e85 to 83dc940 Compare May 18, 2025 02:00
@cfriedt cfriedt changed the title timeutil: correct position of doxygen end-group comment kernel: add k_clock api May 18, 2025
@cfriedt cfriedt changed the title kernel: add k_clock api kernel: add k_clock api: remove posix dependency from iso c time May 18, 2025
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch 13 times, most recently from db7b057 to fe430cf Compare May 18, 2025 17:57
@cfriedt
Copy link
Member Author

cfriedt commented May 18, 2025

  • adding tests for timespec_to_timeout() and timeout_from_timespec()

I'd like to get those in better shape before marking this ready for review.

@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch from fe430cf to 9a4203b Compare May 20, 2025 18:02
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch 2 times, most recently from 5078a47 to 730558c Compare May 20, 2025 20:41
@peter-mitsis
Copy link
Collaborator

Question ... would the proposed k_clock APIs be better served somewhere under /subsys as something like sys_clock APIs? I am wary of them being placed under /kernel.

@cfriedt
Copy link
Member Author

cfriedt commented May 21, 2025

Question ... would the proposed k_clock APIs be better served somewhere under /subsys as something like sys_clock APIs? I am wary of them being placed under /kernel.

sys_clock could maybe work for CLOCK_MONOTONIC and CLOCK_REALTIME, although I think there is a chance that it could start to creep into the kernel as well, if things like per-cpu clocks or CLOCK_THREAD_CPUTIME_ID or CLOCK_PROCESS_CPUTIME_ID become a thing, which is probable.

It might be possible to reuse existing APIs for calculating some of those things though.

@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch 2 times, most recently from 793d367 to 19947db Compare May 21, 2025 22:24
@cfriedt cfriedt changed the title kernel: add k_clock api: remove posix dependency from iso c time sys: clock: add sys_clock api and remove posix from iso c time May 22, 2025
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch 3 times, most recently from 370277c to c5bbd4f Compare May 22, 2025 04:19
cfriedt added 11 commits June 16, 2025 19:11
Use the implementation of time() from the common libc, since there
it no longer pulls in POSIX.

Use is implied for minimal, newlib, and picolibc, and selected
for IAR.

Signed-off-by: Chris Friedt <[email protected]>
Reduce the dependency on POSIX by taking advantage of the newly added
sys_clock_nanosleep().

Signed-off-by: Chris Friedt <[email protected]>
The C standard does not guarantee that `thrd_success` is equal to
zero, so ensure that the test takes that into account.

Signed-off-by: Chris Friedt <[email protected]>
The specification does not say that the first struct timespec passed to
thrd_sleep() may be NULL.

Preserving the testpoint so that a possible future testsuite can be
run that expects faults to occur when undefined behaviour is used.

Faults are one possible solution to handling undefined behaviour, but
it would be good to come to a concensus about how that should be
handled in Zephyr for standard function calls belonging to ISO C or
POSIX.

Signed-off-by: Chris Friedt <[email protected]>
Use `timespec_from_timeout(K_MSEC(msec), &ts)` instead of leaning on
lazily-crafted timespecs with invalid tv_nsec fields.

Signed-off-by: Chris Friedt <[email protected]>
Use sys_clock_gettime() instead of clock_gettime() and use
timespec_add() instead of manually adding timespec structures.

Signed-off-by: Chris Friedt <[email protected]>
Presumably the time testsuite was separate from the c library set of
testsuites because it had a depedency on POSIX.

Since that dependency no longer exists, colocate the time testsuite
with the other c library testsuites.

Signed-off-by: Chris Friedt <[email protected]>
Use the newly added sys_clock API in lib/os for

* clock_gettime()
* clock_settime()
* clock_nanosleep() and nanosleep()
* gettimeofday()

Signed-off-by: Chris Friedt <[email protected]>
The ISO C function time() should not depend on POSIX and this was
corrected recently via the common libc time() implementation.

Remove this dependency from the network subsystem where it has
been unduly needed for some time.

Similarly, XSI_SINGLE_PROCESS was a dependency for time() via
picolibc, because the time() implementation in picolibc relies
on the POSIX gettimeofday() call.

However, since Zephyr's common libc time() implementation no longer
depends on that, it can be removed as a dependency in the network
subsystem as well.

Signed-off-by: Chris Friedt <[email protected]>
Remove POSIX_TIMERS and XSI_SINGLE_PROCESS dependencies from the aws
cloud sample and the lwm2m client sample, as they are no longer
required.

Signed-off-by: Chris Friedt <[email protected]>
Merge sys_clock.h into sys/clock.h and preserve the original header.

This allows us to use a slightly more intuitive name for the
sys/clock.h header, while reducing churn throughout the codebase.

Signed-off-by: Chris Friedt <[email protected]>
@cfriedt cfriedt dismissed stale reviews from maass-hamburg and ycsin via ee874a7 June 16, 2025 23:12
@cfriedt cfriedt force-pushed the remove-posix-from-time-equation branch from 400c18a to ee874a7 Compare June 16, 2025 23:12
@cfriedt cfriedt requested review from ycsin and maass-hamburg June 16, 2025 23:12
Copy link

@cfriedt
Copy link
Member Author

cfriedt commented Jun 17, 2025

@nashif - this still requires assignee approval

@cfriedt
Copy link
Member Author

cfriedt commented Jun 18, 2025

@andyross - you're also listed as a maintainer of Base OS, so I've added you as an assignee.

I think (?) either @nashif or @andyross 's approvals should unblock this in the merge list?

@kartben kartben merged commit c08544a into zephyrproject-rtos:main Jun 24, 2025
36 checks passed
@cfriedt cfriedt deleted the remove-posix-from-time-equation branch June 24, 2025 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Base OS Base OS Library (lib/os) area: C Library C Standard Library area: LWM2M area: Networking area: POSIX POSIX API Library area: Process area: Samples Samples area: Tests Issues related to a particular existing or missing test area: Utilities Release Notes To be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

posix: Without configuring POSIX_API, it is impossible to configure POSIX_TIMER libc: picolibc + common: time() should not refer to POSIX functions